package com.zj.common.utils

import android.app.Activity
import java.util.*

object ActivityManage {

    private val stack: Stack<Activity> = Stack()

    fun push(activity: Activity) {
        stack.push(activity)
    }

    fun removeActivity(activity: Activity) {
        if (stack.contains(activity)) {
            activity.finish()
            stack.remove(activity)
        }
    }

    fun currentActivity(): Activity? {
        return if (stack.isEmpty()) {
            null
        } else stack.peek()
    }

    /**
     * 获取最上面的Activity
     */
    fun getTopActivity(): Activity {
        return stack.lastElement()
    }

    fun clearActivity() {
        for (index in stack.indices) {
            stack[index].finish()
        }
        stack.clear()
    }

}